package com.cn.auth.controller.blog;

import com.cn.auth.common.*;
import com.cn.auth.po.blog.Blog;
import com.cn.auth.po.blog.Comment;
import com.cn.auth.po.blog.Notice;
import com.cn.auth.service.blog.BlogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * describe: 博客
 * current user Maochao.zhu
 * current system 2020/1/19
 */
@Controller
@RequestMapping("/blog")
public class BlogController {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    BlogService blogService;

    @RequestMapping("index")
    public ModelAndView index() {
        ModelAndView modelView = new ModelAndView();
        List<Blog> list = blogService.getBlogList(null);
        modelView.addObject("list",list);
        modelView.addObject("notice",blogService.getNoticeTop1ByType(1));
        modelView.setViewName("blog/index");
        return modelView;
    }
    @RequestMapping("comment")
    public ModelAndView comment(Integer id) {
        ModelAndView modelView = new ModelAndView();
        Blog blog =blogService.getBlogById(id);
        modelView.addObject("blog",blog);
        modelView.setViewName("blog/comment");
        return modelView;
    }
    @RequestMapping("about")
    public ModelAndView about() {
        ModelAndView modelView = new ModelAndView();
        modelView.addObject("notice",blogService.getNoticeTop1ByType(2));
        modelView.setViewName("blog/about");
        return modelView;
    }
    @RequestMapping("details")
    public ModelAndView details(Integer id) {
        ModelAndView modelView = new ModelAndView();
        Blog blog =blogService.getBlogById(id);
        modelView.addObject("blog",blog);
        List<Comment> commentList = blogService.getCommetByBlogId(id);
        modelView.addObject("commentList",commentList);
        modelView.setViewName("blog/details");
        return modelView;
    }
    @RequestMapping("message")
    public ModelAndView message() {
        ModelAndView modelView = new ModelAndView();
        List<Comment> commentList = blogService.getCommetByBlogId(null);
        modelView.addObject("commentList",commentList);
        modelView.setViewName("blog/message");
        return modelView;
    }

    @RequestMapping("commitComment")
    public String commitComment(Comment comment,HttpServletRequest request) {
        if(StringUtil.isBlank(comment.getCreateUser())){
            comment.setCreateUser("匿名用户"+ UserUtils.getIpAddr(request));
        }
        if(StringUtil.isNotBlank(comment.getContent())){
            blogService.insertComment(comment);
        }
        return "redirect:/blog/message";
    }

    /**
     * 去首页
     *
     * @return
     */
    @RequestMapping("goIndex")
    public ModelAndView goIndex() {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("blog/blogIndex");
        return modelView;
    }

    @RequestMapping("goAdd")
    public ModelAndView goAdd() {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName("blog/blogAdd");
        return modelView;
    }

    @RequestMapping("goEdit")
    public ModelAndView goEdit(Integer id) {
        Blog blog = blogService.getBlogById(id);
        ModelAndView modelView = new ModelAndView();
        modelView.addObject("blog", blog);
        modelView.setViewName("blog/blogEdit");
        return modelView;
    }

    /**
     * 获取列表
     *
     * @return
     */
    @RequestMapping("getBlogList")
    @ResponseBody
    public BasePageData getBlogList(Blog blog) {
        BasePageData data = new BasePageData();
        List<Blog> list = blogService.getBlogList(blog);
        Integer count = blogService.getBlogListCount(blog);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        data.setData(list);
        return data;
    }

    @RequestMapping("insertBlog")
    @ResponseBody
    public BasePageData insertBlog(Blog blog) {
        BasePageData data = new BasePageData();
        Integer count = blogService.insertBlog(blog);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }

    @RequestMapping("updateBlog")
    @ResponseBody
    public BasePageData updateBlog(Blog blog) {
        BasePageData data = new BasePageData();
        Integer count = blogService.updateBlog(blog);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }

    @RequestMapping("delteBlogById")
    @ResponseBody
    public BasePageData deleteBlog(String id) {
        BasePageData data = new BasePageData();
        String[] ids = id.split(",");
        Integer count = blogService.delteBlogById(ids);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }

    @RequestMapping("addBlogReadCount")
    @ResponseBody
    public BasePageData addBlogReadCount(Integer id) {
        BasePageData data = new BasePageData();
        Integer count = blogService.addBlogReadCount(id);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }

    @RequestMapping("addBlogPointsCount")
    @ResponseBody
    public BasePageData addBlogPointsCount(Integer id) {
        BasePageData data = new BasePageData();
        Integer count = blogService.addBlogPointsCount(id);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }


    /**
     * 上传图片
     *
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("uploadImage")
    @ResponseBody
    public BasePageData uploadImage(@RequestParam(value = "file") MultipartFile file,
                                    HttpServletRequest request, HttpServletResponse response) {
        BasePageData data = new BasePageData();
        try {
            String filePath = FileUtil.uploadImage("image", file, request, response);
            if (null != filePath) {
                data.setCode(WebResponseCode.SUCCESS);
                data.setMsg(filePath);
            } else {
                data.setCode(WebResponseCode.FAIL);
                data.setMsg("上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            data.setCode(WebResponseCode.FAIL);
            data.setMsg("操作失败!");
        }
        return data;
    }

//...
@RequestMapping("goCommentIndex")
public ModelAndView goCommentIndex() {
    ModelAndView modelView = new ModelAndView();
    modelView.setViewName("blog/commentIndex");
    return modelView;
}

@RequestMapping("goNoticeIndex")
public ModelAndView goNoticeIndex() {
    ModelAndView modelView = new ModelAndView();
    modelView.setViewName("blog/noticeIndex");
    return modelView;
}

@RequestMapping("goNoticeAdd")
public ModelAndView goNoticeAdd() {
    ModelAndView modelView = new ModelAndView();
    modelView.setViewName("blog/noticeAdd");
    return modelView;
}

@RequestMapping("goNoticeEdit")
public ModelAndView goNoticeEdit(Integer id) {
    ModelAndView modelView = new ModelAndView();
    Notice notice = blogService.getNoticeById(id);
    modelView.addObject("notice",notice);
    modelView.setViewName("blog/noticeEdit");
    return modelView;
}


    @RequestMapping("deleteCommentByIds")
    @ResponseBody
    public BasePageData deleteCommentByIds(String id) {
        BasePageData data = new BasePageData();
        String[] ids = id.split(",");
        Integer count = blogService.deleteCommentByIds(ids);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }
    @RequestMapping("deleteNoticeByIds")
    @ResponseBody
    public BasePageData deleteNoticeByIds(String id) {
        BasePageData data = new BasePageData();
        String[] ids = id.split(",");
        Integer count = blogService.deleteNoticeByIds(ids);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }

    @RequestMapping("getCommetList")
    @ResponseBody
    public BasePageData getCommetList(Comment comment) {
        BasePageData data = new BasePageData();
        List<Map<String,Object>> list = blogService.getCommetList(comment);
        Integer count = blogService.getCommetListCount(comment);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        data.setData(list);
        return data;
    }
    @RequestMapping("getNoticeList")
    @ResponseBody
    public BasePageData getNoticeList(Notice notice) {
        BasePageData data = new BasePageData();
        List<Notice> list = blogService.getNoticeList(notice);
        Integer count = blogService.getNoticeListCount(notice);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        data.setData(list);
        return data;
    }

    @RequestMapping("insertNotice")
    @ResponseBody
    public BasePageData insertNotice(Notice notice) {
        BasePageData data = new BasePageData();
        Integer count = blogService.insertNotice(notice);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }

    @RequestMapping("updateNoticeById")
    @ResponseBody
    public BasePageData updateNoticeById(Notice notice) {
        BasePageData data = new BasePageData();
        Integer count = blogService.updateNoticeById(notice);
        data.setCode(WebResponseCode.SUCCESS);
        data.setMsg("操作成功!");
        data.setCount(count);
        return data;
    }
}
